<html><head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title></title><link rel="stylesheet" href="reference.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.62.4"><link rel="home" href="index.html" title=""><link rel="up" href="publishing.html" title=""><link rel="previous" href="ch01s03.html" title=""><link rel="next" href="extensions.html" title=""></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center"></th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch01s03.html"></a>&nbsp;</td><th width="60%" align="center"></th><td width="20%" align="right">&nbsp;<a accesskey="n" href="extensions.html"></a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d0e734"></a></h2></div></div><div></div></div><p>The DocBook XSL stylesheets are written in a modular
fashion. Each of the HTML and FO stylesheets starts with a
driver file that assembles a collection of component files
into a complete stylesheet. This modular design puts similar things together into smaller files that are easier to write and maintain than one big stylesheet. The modular stylesheet files
are distributed among four directories:</p><div class="variablelist"><dl><dt><span class="term">common/</span></dt><dd><p>contains code common to both stylesheets, including localization data
</p></dd><dt><span class="term">fo/</span></dt><dd><p>a stylesheet that produces XSL FO result trees
</p></dd><dt><span class="term">html/</span></dt><dd><p>a stylesheet that produces HTML/XHTML result trees
</p></dd><dt><span class="term">lib/</span></dt><dd><p>contains schema-independent functions
</p></dd></dl></div><p>The driver files for each of HTML and FO stylesheets
are <tt class="filename">html/docbook.xsl</tt> and <tt class="filename">fo/docbook.xsl</tt>,
respectively. A driver file consists mostly of a bunch
of <tt class="literal">&lt;xsl:include&gt;</tt> instructions to
pull in the component templates, and then defines some
top-level templates. For example:</p><pre class="programlisting">&lt;xsl:include href="../VERSION"/&gt;
&lt;xsl:include href="../lib/lib.xsl"/&gt;
&lt;xsl:include href="../common/l10n.xsl"/&gt;
&lt;xsl:include href="../common/common.xsl"/&gt;
&lt;xsl:include href="autotoc.xsl"/&gt;
&lt;xsl:include href="lists.xsl"/&gt;
&lt;xsl:include href="callout.xsl"/&gt;
...
&lt;xsl:include href="param.xsl"/&gt;
&lt;xsl:include href="pi.xsl"/&gt;
</pre><p>The first four modules are shared with the FO
stylesheet and are referenced using relative pathnames to
the common directories. Then the long list of component
stylesheets starts. Pathnames in include statements are
always taken to be relative to the including file. Each
included file must be a valid XSL stylesheet, which means
its root element must
be <tt class="literal">&lt;xsl:stylesheet&gt;</tt>.</p><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d0e782"></a></h3></div></div><div></div></div><p>XSL actually provides two inclusion
mechanisms: <tt class="literal">&lt;xsl:include&gt;</tt> and <tt class="literal">&lt;xsl:import&gt;</tt>.
Of the two, <tt class="literal">&lt;xsl:include&gt;</tt> is
the simpler. It treats the included content as if it were
actually typed into the file at that point, and doesn't
give it any more or less precedence relative to the
surrounding text. It is best used when assembling
dissimilar templates that don't overlap what they match.
The DocBook driver files use this instruction to assemble a
set of modules into a stylesheet.</p><p>In contrast, <tt class="literal">&lt;xsl:import&gt;</tt> lets
you manage the precedence of templates and variables. It is
the preferred mode of customizing another stylesheet because
it lets you override definitions in the distributed
stylesheet with your own, without altering the distribution
files at all. You simply import the whole stylesheet and
add whatever changes you want.</p><p>The precedence rules for import are detailed and
rigorously defined in the XSL standard. The basic rule is
that any templates and variables in the importing
stylesheet have precedence over equivalent templates and
variables in the imported stylesheet. Think of the imported stylesheet elements as a fallback collection, to be used only if a match is not found in the current stylesheet. You can customize the templates you want to change in your stylesheet file, and let the imported stylesheet handle the rest.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"></h3><p>Customizing a DocBook XSL stylesheet is the opposite
of customizing a DocBook DTD. When you customize a DocBook
DTD, the rules of XML and SGML dictate that
the <span class="emphasis"><em>first</em></span> of any duplicate declarations
wins. Any subsequent declarations of the same element or
entity are ignored. The architecture of the DTD provides
slots for inserting your own custom declarations early
enough in the DTD for them to override the standard
declarations. In contrast, customizing an XSL stylesheet is
simpler because your definitions have precedence over imported ones.</p></div><p>You can carry modularization to deeper levels because
module files can also include or import other modules.
You'll need to be careful to maintain the precedence that
you want as the modules get rolled up into a complete
stylesheet. </p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d0e811"></a></h3></div></div><div></div></div><p>There is currently one example of customizing
with <tt class="literal">&lt;xsl:import&gt;</tt> in the HTML
version of the DocBook stylesheets.
The <tt class="filename">xtchunk.xsl</tt> stylesheet modifies the
HTML processing to output many smaller HTML files rather
than a single large file per input document. It uses XSL
extensions defined only in the XSL
processor <span><b class="command">XT</b></span>. In the driver
file <tt class="filename">xtchunk.xsl</tt>, the first instruction
is <tt class="literal">&lt;xsl:import
href="docbook.xsl"/&gt;</tt>. That instruction imports
the original driver file, which in turn uses
many <tt class="literal">&lt;xsl:include&gt;</tt> instructions to
include all the modules. That single import instruction
gives the new stylesheet the complete set of DocBook
templates to start with.</p><p>After the
import, <tt class="filename">xtchunk.xsl</tt> redefines some of
the templates and adds some new ones. Here is one example
of a redefined template:</p><pre class="programlisting"><i class="lineannotation"><span class="lineannotation">Original template in autotoc.xsl</span></i>
&lt;xsl:template name="href.target"&gt;
  &lt;xsl:param name="object" select="."/&gt;
  &lt;xsl:text&gt;#&lt;/xsl:text&gt;
  &lt;xsl:call-template name="object.id"&gt;
    &lt;xsl:with-param name="object" select="$object"/&gt;
  &lt;/xsl:call-template&gt;
&lt;/xsl:template&gt;

<i class="lineannotation"><span class="lineannotation">New template in xtchunk.xsl</span></i>
&lt;xsl:template name="href.target"&gt;
  &lt;xsl:param name="object" select="."/&gt;
  &lt;xsl:variable name="ischunk"&gt;
    &lt;xsl:call-template name="chunk"&gt;
      &lt;xsl:with-param name="node" select="$object"/&gt;
    &lt;/xsl:call-template&gt;
  &lt;/xsl:variable&gt;

  &lt;xsl:apply-templates mode="chunk-filename" select="$object"/&gt;

  &lt;xsl:if test="$ischunk='0'"&gt;
    &lt;xsl:text&gt;#&lt;/xsl:text&gt;
    &lt;xsl:call-template name="object.id"&gt;
      &lt;xsl:with-param name="object" select="$object"/&gt;
    &lt;/xsl:call-template&gt;
  &lt;/xsl:if&gt;
&lt;/xsl:template&gt;
</pre><p>The new template handles the more complex processing
of HREFs when the output is split into many HTML files.
Where the old template could simply
output <tt class="literal">#<i class="replaceable"><tt>object.id</tt></i></tt>,
the new one outputs <tt class="literal"><i class="replaceable"><tt>filename</tt></i>#<i class="replaceable"><tt>object.id</tt></i></tt>.</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d0e862"></a></h3></div></div><div></div></div><p>You may not have to define any new templates,
however. The DocBook stylesheets are parameterized using
XSL variables rather than hard-coded values for many of the
formatting features. Since
the <tt class="literal">&lt;xsl:import&gt;</tt> mechanism also
lets you redefine global variables, this gives you an easy
way to customize many features of the DocBook
stylesheets. Over time, more features will be parameterized to permit customization. If you find hardcoded values in the stylesheets that would be useful to customize, please let the maintainer know.</p><p>Near the end of the list of includes in the main
DocBook driver file is the
instruction <tt class="literal">&lt;xsl:include
href="param.xsl"/&gt;</tt>.
The <tt class="filename">param.xsl</tt> file is the most
important module for customizing a DocBook XSL stylesheet.
This module contains no templates, only definitions of
stylesheet variables. Since these variables are defined
outside of any template, they are global variables and
apply to the entire stylesheet. By redefining these
variables in an importing stylesheet, you can change the
behavior of the stylesheet.</p><p>To create a customized DocBook stylesheet, you simply
create a new stylesheet file such
as <tt class="filename">mystyle.xsl</tt> that imports the standard
stylesheet and adds your own new variable definitions. Here
is an example of a complete custom stylesheet that changes
the depth of sections listed in the table of contents from
two to three:</p><pre class="programlisting">&lt;?xml version='1.0'?&gt;
&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version='1.0'
                xmlns="http://www.w3.org/TR/xhtml1/transitional"
                exclude-result-prefixes="#default"&gt;

&lt;xsl:import href="docbook.xsl"/&gt;

&lt;xsl:variable name="toc.section.depth"&gt;3&lt;/xsl:variable&gt;
&lt;!-- Add other variable definitions here --&gt;

&lt;/xsl:stylesheet&gt;
</pre><p>Following the opening stylesheet element are the
import instruction and one variable definition. The
variable <tt class="literal">toc.section.depth</tt> was defined
in <tt class="filename">param.xsl</tt> with value "2", and here
it is defined as "3". Since the importing stylesheet takes
precedence, this new value is used. Thus documents
processed with <tt class="filename">mystyle.xsl</tt> instead
of <tt class="filename">docbook.xsl</tt> will have three levels
of sections in the tables of contents, and all other
processing will be the same.</p><p>Use the list of variables
in <tt class="filename">param.xsl</tt> as your guide for creating
a custom stylesheet. If the changes you want are controlled
by a variable there, then customizing is easy. </p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d0e904"></a></h3></div></div><div></div></div><p>If the changes you want are more extensive than what
is supported by variables, you can write new templates. You
can put your new templates directly in your importing
stylesheet, or you can modularize your importing stylesheet
as well. You can write your own stylesheet module
containing a collection of templates for processing lists,
for example, and put them in a file
named <tt class="filename">mylists.xsl</tt>. Then your importing
stylesheet can pull in your list templates with
a <tt class="literal">&lt;xsl:include
href="mylists.xsl"/&gt;</tt> instruction. Since your
included template definitions appear after the main import
instruction, your templates will take precedence.</p><p>You'll need to make sure your new templates are
compatible with the remaining modules, which means:</p><div class="itemizedlist"><ul type="disc"><li><p>Any named templates should use the same name so
calling templates in other modules can find them.</p></li><li><p>Your template set should process the same elements
matched by templates in the original module, to ensure
complete coverage.</p></li><li><p>Include the same set
of <tt class="literal">&lt;xsl:param&gt;</tt> elements in each
template to interface properly with any calling templates,
although you can set different values for your
parameters.</p></li><li><p>Any templates that are used like subroutines to
return a value should return the same data type.</p></li></ul></div></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d0e933"></a></h3></div></div><div></div></div><p>Another approach to customizing the stylesheets is to
write your own driver file. Instead of
using <tt class="literal">&lt;xsl:import
href="docbook.xsl"/&gt;</tt>, you copy that file to a
new name and rewrite any of
the <tt class="literal">&lt;xsl:include/&gt;</tt> instructions to
assemble a custom collection of stylesheet modules. One
reason to do this is to speed up processing by reducing the
size of the stylesheet. If you are using a customized
DocBook DTD that omits many elements you never use, you
might be able to omit those modules of the
stylesheet.</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d0e944"></a></h3></div></div><div></div></div><p>The DocBook stylesheets include features for
localizing generated text, that is, printing any generated
text in a language other than the default English. In
general, the stylesheets will switch to the language
identified by a <tt class="literal">lang</tt> attribute when
processing elements in your documents. If your documents
use the <tt class="literal">lang</tt> attribute, then you don't
need to customize the stylesheets at all for
localization.</p><p>As far as the stylesheets go,
a <tt class="literal">lang</tt> attribute is inherited by the
descendents of a document element. The stylesheet searches
for a <tt class="literal">lang</tt> attribute using this XPath
expression:</p><pre class="programlisting">&lt;xsl:variable name="lang-attr"
         select="($target/ancestor-or-self::*/@lang
                  |$target/ancestor-or-self::*/@xml:lang)[last()]"/&gt;</pre><p>This locates the attribute on the current element or
its most recent ancestor. Thus
a <tt class="literal">lang</tt> attribute is in effect for an
element and all of its descendents, unless it is reset in
one of those descendents. If you define it in only your
document root element, then it applies to the whole
document:</p><pre class="programlisting">&lt;?xml version="1.0"?&gt;
&lt;!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.0//EN" "docbook.dtd"&gt;
&lt;book lang="fr"&gt;
...
&lt;/book&gt;</pre><p>When text is being generated, the stylesheet checks
the most recent <tt class="literal">lang</tt> attribute and looks
up the generated text strings for that language in a
localization XML file. These are located in
the <tt class="filename">common</tt> directory of the
stylesheets, one file per language. Here is the top of the
file <tt class="filename">fr.xml</tt>:</p><pre class="programlisting">&lt;localization language="fr"&gt;

&lt;gentext key="abstract"                 text="R&amp;#x00E9;sum&amp;#x00E9;"/&gt;
&lt;gentext key="answer"                   text="R:"/&gt;
&lt;gentext key="appendix"                 text="Annexe"/&gt;
&lt;gentext key="article"                  text="Article"/&gt;
&lt;gentext key="bibliography"             text="Bibliographie"/&gt;
...
</pre><p>The stylesheet templates use the gentext key names,
and then the stylesheet looks up the associated text value
when the document is processed with that lang setting. The
file <tt class="filename">l10n.xml</tt> (note
the <tt class="filename">.xml</tt> suffix) lists the filenames of
all the supported languages.</p><p>You can also create a custom stylesheet that sets the
language. That might be useful if your documents don't make
appropriate use of the <tt class="literal">lang</tt> attribute.
The module <tt class="filename">l10n.xsl</tt> defines two global
variables that can be overridden with an importing
stylesheet as described above. Here are their default
definitions:</p><pre class="programlisting">&lt;xsl:variable name="l10n.gentext.language"&gt;&lt;/xsl:variable&gt;
&lt;xsl:variable name="l10n.gentext.default.language"&gt;en&lt;/xsl:variable&gt;
</pre><p>The first one sets the language for all elements,
regardless of an element's <tt class="literal">lang</tt> attribute
value. The second just sets a default language for any
elements that haven't got a <tt class="literal">lang</tt> setting
of their own (or their ancestors).</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch01s03.html"></a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="publishing.html"></a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="extensions.html"></a></td></tr><tr><td width="40%" align="left" valign="top">&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html"></a></td><td width="40%" align="right" valign="top">&nbsp;</td></tr></table></div></body></html>